package cn.ondraw.ragflow.service.impl;


import cn.hutool.json.JSONUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import cn.ondraw.ragflow.pojo.CommonResponse;
import cn.ondraw.ragflow.pojo.request.CreateChatAssistantRequest;
import cn.ondraw.ragflow.pojo.request.DeleteChatAssistantRequest;
import cn.ondraw.ragflow.pojo.request.ListChatAssistantRequest;
import cn.ondraw.ragflow.pojo.request.UpdateChatAssistantRequest;
import cn.ondraw.ragflow.pojo.response.CreateChatAssistantResponse;
import cn.ondraw.ragflow.pojo.response.ListChatAssistantResponse;
import cn.ondraw.ragflow.service.AssistantRagFlowService;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

/**
 * 聊天助手服务实现类
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class AssistantRagFlowServiceImpl implements AssistantRagFlowService {

    private final PythonApiCaller pythonApiCaller;

    private final String URL_PREFIX = "/api/v1/chats";

    @Override
    public CreateChatAssistantResponse createChatAssistant(CreateChatAssistantRequest request) {
        String apiUrl = URL_PREFIX;
        ResponseEntity<?> response = pythonApiCaller.callPostApi(apiUrl, request);
        return JSONUtil.toBean(response.getBody() != null ? response.getBody().toString() : null, CreateChatAssistantResponse.class, true);
    }

    @Override
    public CommonResponse updateChatAssistant(String chatId, UpdateChatAssistantRequest request) {
        String apiUrl = URL_PREFIX + chatId;
        ResponseEntity<?> response = pythonApiCaller.callPutApi(apiUrl, request);
        return JSONUtil.toBean(response.getBody() != null ? response.getBody().toString() : null, CommonResponse.class, true);
    }

    @Override
    public CommonResponse deleteChatAssistants(DeleteChatAssistantRequest request) {
        String apiUrl = URL_PREFIX;
        ResponseEntity<?> response = pythonApiCaller.callDeleteApi(apiUrl, request);
        return JSONUtil.toBean(response.getBody() != null ? response.getBody().toString() : null, CommonResponse.class, true);
    }

    @Override
    public ListChatAssistantResponse listChatAssistants(ListChatAssistantRequest request) {
        StringBuilder urlBuilder = new StringBuilder(URL_PREFIX + "?");
        if (request.getId() != null) {
            urlBuilder.append("id=").append(request.getId()).append("&");
        }
        if (request.getName() != null) {
            urlBuilder.append("name=").append(request.getName()).append("&");
        }
        if (request.getPage() != null) {
            urlBuilder.append("page=").append(request.getPage()).append("&");
        }
        if (request.getPage_size() != null) {
            urlBuilder.append("page_size=").append(request.getPage_size()).append("&");
        }
        if (request.getOrderby() != null) {
            urlBuilder.append("orderby=").append(request.getOrderby()).append("&");
        }
        if (request.getDesc() != null) {
            urlBuilder.append("desc=").append(request.getDesc()).append("&");
        }
        if (urlBuilder.charAt(urlBuilder.length() - 1) == '&') {
            urlBuilder.deleteCharAt(urlBuilder.length() - 1);
        }
        String url = urlBuilder.toString();
        ResponseEntity<?> response = pythonApiCaller.callGetApi(url);
        return JSONUtil.toBean(response.getBody() != null ? response.getBody().toString() : null, ListChatAssistantResponse.class, true);
    }
}
